﻿using UnityEngine;
using UnityEditor;

using System.Text.RegularExpressions;

public class TextureImport : AssetPostprocessor {

	static string RawImagePath = "RawTextures/";

	/// <summary>
	///预处理贴图，可以自动设置导入参数
	/// </summary>
	public void OnPreprocessTexture()
	{
		ProcessRawTextures ();
	}

	/// <summary>
	/// 1.放入RawTextures下的贴图，类型设置为Sprite
	/// 2.根据RawTextures下的分类文件夹名字，设置Packing Tag
	/// </summary>
	public void ProcessRawTextures()
	{
		TextureImporter texImporter = this.assetImporter as TextureImporter;
		if (this.assetPath.Contains (RawImagePath)) {
			texImporter.textureType = TextureImporterType.Sprite;

			int startIndex = this.assetPath.LastIndexOf (RawImagePath) + RawImagePath.Length;
			if (this.assetPath.LastIndexOf ('/') - startIndex <= 0) {
				Debug.Log ("作为单个图片打包,要打图集请放到文件夹下;资源路径:" + this.assetPath);
				return;
			}

			//根据文件夹设置packTag，打成图集
			string packTag = this.assetPath.Substring (startIndex, this.assetPath.LastIndexOf ('/') - startIndex).ToLower();
			texImporter.spritePackingTag = packTag;

			//图集打包名字
			string abName = "ui_atlas/" + packTag + ".unity3d";
			texImporter.assetBundleName = abName;

			Debug.Log ("导入贴图:"+ this.assetPath+";图集名:"+packTag+";bundle名:"+abName);
		}
	}
}
